#include "qwtexteditdelegate.h"
#include <QTextEdit>
QWTextEditDelegate::QWTextEditDelegate(QObject *parent)
    : QStyledItemDelegate{parent}
{}

QWidget *QWTextEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    // 创建代理编辑组件
    Q_UNUSED(option);
    Q_UNUSED(index);
    QTextEdit *editor = new QTextEdit(parent);
    return editor;
}

void QWTextEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    // 从数据模型中获取数据，显示到代理组件中
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QTextEdit *textEdit = static_cast<QTextEdit*>(editor);
    textEdit->setText(value);
}

void QWTextEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    // 将代理编辑组件的数据保存到数据模型中
    QTextEdit *textEdit = static_cast<QTextEdit*>(editor);
    QString value = textEdit->toPlainText();
    model->setData(index, value, Qt::EditRole);
}

void QWTextEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    // 设置组件大小
    Q_UNUSED(index);
    QRect r = option.rect;
    r.setHeight(r.height() + 100);
    editor->setGeometry(r);
}
